package com.xyh.controller;

import com.google.gson.Gson;
import com.xyh.bean.Area;
import com.xyh.bean.Insect;
import com.xyh.bean.ManagerClass;
import com.xyh.service.IAreaService;
import com.xyh.service.IClassService;
import com.xyh.service.impl.AreaServiceImpl;
import com.xyh.service.impl.ClassServiceImpl;
import com.xyh.util.PageUtil;
import com.xyh.vo.AreaVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: XiongYuhao
 * @Date: 2020/10/25 0025-下午 12:22
 * @Description: MyForestManagerSystem
 * @version：1.0
 */
@WebServlet(name = "AreaServlet",urlPatterns = "/as.do")
public class AreaServlet extends HttpServlet {
    IAreaService ias = new AreaServiceImpl();
    IClassService ics = new ClassServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method.equalsIgnoreCase("area_detail_page")){
            //查询地域详情并分页
            areaDetailByPage(request,response);
        }else if(method.equalsIgnoreCase("add_area")){
            //添加地域信息
            addArea(request,response);
        }else if(method.equalsIgnoreCase("queryAreaList")){
            queryAreaList(request,response);
        }
    }
    private void queryAreaList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Area> areaList = ias.queryAreaList();
        Gson gson = new Gson();
        String json = gson.toJson(areaList);
        response.getWriter().write(json);
    }
    private void addArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String areaName = request.getParameter("areaName");
        String treeType = request.getParameter("treeType");
        String advTree = request.getParameter("advTree");
        String land = request.getParameter("land");
        if(areaName.equalsIgnoreCase("")||treeType.equalsIgnoreCase("")
                ||land.equalsIgnoreCase("")||advTree.equalsIgnoreCase("")){
            return;
        }

        Area area = new Area(areaName,treeType,advTree,land);
        boolean flag = ias.add(area);
        Gson gson = new Gson();
        String json = gson.toJson(flag);
        response.getWriter().write(json);
    }
    private void areaDetailByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pn = request.getParameter("pageNo");
        int pageNo = 1;
        if(pn == null){
            pageNo = 1;
        }else{
            pageNo = Integer.parseInt(pn);
        }
        String ps = request.getParameter("pageSize");
        int pageSize = Integer.parseInt(ps);
        String areaName = request.getParameter("areaName");
        String treeType = request.getParameter("treeType");
        String className = request.getParameter("className");
        PageUtil<AreaVO> pageBean = ias.listByPage(pageNo,pageSize,areaName,treeType,className);
        Gson gson = new Gson();
        String json = gson.toJson(pageBean);
        response.getWriter().write(json);
    }
}
